package ar.com.msebuilder;

import ar.com.msebuilder.SVNRevisionsFilter.SVNRevFilterTypes;

public class MSEBuilder {
	
	public enum Operation {
		SVNDOWNLOAD, MSECREATION, ALL 
	}
	
	private static final String DEFAULT_DIR = ".";
	private String svnUrl = "";
	private String svnUser = "";
	private String svnPwd = "";
	private SVNRevFilterTypes svnFilter = null;
	private long minRevision = 0;
	private long maxRevision = 0;
	private String svnDestDir = DEFAULT_DIR;
	private String mseDestDir = DEFAULT_DIR;
	private boolean deleteSVNFiles = false;
	private int threadsNum = 1;
	private Operation operation = Operation.ALL;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// If no arguments were given to the application show an error message
		if (args.length < 4) {
			System.out.println("\nSOME ARGUMENTS MISSING. PLEASE ADD THEM.");
			showExampleMessage();
		}
		// Create MSE Builder object with the parameter values given by the user
		MSEBuilder mseBuilder = new MSEBuilder(args);
		// Execute the SVN download and MSE files creation
		mseBuilder.execute();
	}
	
	public MSEBuilder(String[] args) {
		for (String arg : args) {
			if (arg.startsWith("--url=http") || arg.startsWith("--url=svn") || 
				arg.startsWith("--url=file")) {
				svnUrl = arg.split("=")[1];
			} else if (arg.matches("--user=[^$].*")) {
				svnUser = arg.split("=")[1];
			} else if (arg.matches("--pwd=[^$].*")) {
				svnPwd = arg.split("=")[1];
			} else if (arg.matches("--minrev=[^$].*") && arg.matches("--minrev=[0-9]*")) {
				minRevision = Long.valueOf(arg.split("=")[1]);
			} else if (arg.matches("--maxrev=[^$].*") && arg.matches("--maxrev=[0-9]*")) {
				maxRevision = Long.valueOf(arg.split("=")[1]);
			} else if (arg.equals("--svnfilter=revisions") || arg.equals("--svnfilter=daily") ||
					arg.equals("--svnfilter=weekly") || arg.equals("--svnfilter=monthly")) {
				String svnFilterStr = arg.split("=")[1];
				svnFilter = SVNRevFilterTypes.valueOf(svnFilterStr.toUpperCase());
			} else if (arg.matches("--svndest=[^$].*")) {
				svnDestDir = arg.split("=")[1];
			} else if (arg.matches("--msedest=[^$].*")) {
				mseDestDir = arg.split("=")[1];
			} else if (arg.equals("--delsvnfiles")) {
				deleteSVNFiles = true;
			} else if (arg.matches("--threadsnum=[^$].*")) {
				threadsNum = Integer.valueOf(arg.split("=")[1]);
			} else if (arg.equals("--operation=svndownload") || 
					arg.equals("--operation=msecreation") || arg.equals("--operation=all")) {
				operation = Operation.valueOf(arg.split("=")[1].toUpperCase());
			} else {
				System.out.println("\nWRONG OR INCOMPLETE PARAMETER: \"" + arg + "\".");
				showExampleMessage();				
			}
		}
		if (svnUrl.equals("")) {
			System.out.println("\nSVN URL MISSING. PLEASE ADD IT.");
			showExampleMessage();
		}
		if (minRevision <= 0) {
			System.out.println("\nMIN REVISION MISSING. PLEASE ADD IT.");
			showExampleMessage();			
		}
		if (maxRevision <= 0) {
			System.out.println("\nMAX REVISION MISSING. PLEASE ADD IT.");
			showExampleMessage();			
		}
		if (maxRevision < minRevision) {
			System.out.println("\nMAX REVISION IS LOWER THAN MIN REVISION. PLEASE FIX IT.");
			showExampleMessage();			
		}
		if(threadsNum <= 0) {
			System.out.println("\nTHE THREADS NUMBER SPECIFIED IS NOT VALID. PLEASE CHOOSE A NUMBER GREATER THAN 0.");
			showExampleMessage();				
		}
	}
	
	private static void showExampleMessage() {
		System.out.println("\nExample:\n");
		System.out.println("java -jar msebuilder.jar --url=<SVN-URL> " +
				"--minrev=<SVN-REVISION-TO-START> " +
				"--maxrev=<SVN-LAST-REVISION-TO-DOWNLOAD> " +
				"--svnfilter={revisions|daily|weekly|monthly} " +
				"[--user=<SVN-USER-NAME>]" +
				"[--pwd=<SVN-USER-PASSWORD>] [--svndest=<DIR-TO-DOWNLOAD-REVISIONS>] " +
				"[--msedest=<DIR-TO-SAVE-MSE-FILES>] " +
				"[--threadsnum=<NUMBER-OF-PARALLEL-THREADS>] " +
				"[--operation={svndownload|msecreation|all}] [--delsvnfiles] ");			
		System.out.println("\nDefault values for optional arguments:");
		System.out.println("\t--user=\"\"");			
		System.out.println("\t--pwd=\"\"");			
		System.out.println("\t--svndest=\".\"");			
		System.out.println("\t--msedest=\".\"");
		System.out.println("\t--threadsnum=\"1\"");
		System.out.println("\t--operation=\"all\"\n");		
		System.exit(1);
	}
	
	private void execute(){
		// Create a connection to the SVN repository and a threads manager 
		// that will take care of downloading the revisions and creating the MSE files
		ThreadsManager threadsManager = new ThreadsManager(svnUrl, svnUser, 
				svnPwd, threadsNum, deleteSVNFiles, operation);
		// Download all the revisions and create the MSE files
		threadsManager.downloadRevisionsAndCreateMSEFiles(minRevision, maxRevision, 
				svnDestDir, svnFilter, mseDestDir);
	}
	
}
